package com.cho;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class FileFilter implements FilenameFilter {
	
	private static final List<String> includeFileName = new ArrayList<String>();
	private static final List<String> includeFileNameStartsWith = new ArrayList<String>();
	private static final List<String> includeFileNameEndsWith = new ArrayList<String>();
	private static final List<String> excludeFileName = new ArrayList<String>();
	private static final List<String> excludeFileNameStartsWith = new ArrayList<String>();
	private static final List<String> excludeFileNameEndsWith = new ArrayList<String>();
	
	{
		excludeFileName.add(".svn");
		
	}
	
	@Override
	public boolean accept(File dir, String name) {
			if(dir != null && name != null){
				return mainFilter(name); 
			}
		return false;
	}
	
	private boolean mainFilter(String name){
		return excludeFileNameFilter(name);
			//|| excludeFileNameStartWithFilter(name)
			//|| excludeFileNameEndWithFilter(name)
			//|| includeFileNameFilter(name)
			//|| includeFileNameStartWithFilter(name)
			//|| includeFileNameEndWithFilter(name);
	}
	
	private boolean includeFileNameFilter(String name){
		return fileNameFilter(name, this.includeFileName, true, false);
	}
	
	private boolean includeFileNameStartWithFilter(String name){
		return fileNameStartWithFilter(name, this.includeFileNameStartsWith, true, false);
	}
	
	private boolean includeFileNameEndWithFilter(String name){
		return fileNameEndWithFilter(name, this.includeFileNameEndsWith, true, false);
	}
	
	private boolean excludeFileNameFilter(String name){
		return fileNameFilter(name, this.excludeFileName, false, true);
	}
	
	private boolean excludeFileNameStartWithFilter(String name){
		return fileNameStartWithFilter(name, this.excludeFileNameStartsWith, false, true);
	}
	
	private boolean excludeFileNameEndWithFilter(String name){
		return fileNameEndWithFilter(name, this.excludeFileNameEndsWith, false, true);
	}
	
	private boolean fileNameFilter(String name, final List<String> fileName, boolean findSuccess, boolean findFail){
		for (String fileter : fileName) {
			if(name.indexOf(fileter) > -1){
				return findSuccess;
			}
		}
		return findFail;
	}
	
	private boolean fileNameStartWithFilter(String name, final List<String> fileNameStartsWith, boolean findSuccess, boolean findFail){
		for (String prefix : fileNameStartsWith) {
			if(name.startsWith(prefix)){
				return findSuccess;
			}
		}		
		return findFail;
	}
	
	private boolean fileNameEndWithFilter(String name, final List<String> fileNameEndsWith, boolean findSuccess, boolean findFail){
		for (String suffix : fileNameEndsWith) {
			if(name.endsWith(suffix)){
				return findSuccess;
			}
		}		
		return findFail;
	}
	
}
